package cl.sodimac.buc.helper; 

import java.util.Hashtable;
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
 
import org.xml.sax.helpers.DefaultHandler;
 
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
 
public class XsdValidator 
{ 
      
    private static XsdValidator instance = null;
    private static Hashtable xsds = null;
    
    public static XsdValidator getInstance() throws Exception{
        if (instance == null) instance = new XsdValidator();
        return instance;
    }
    
    public static boolean validacion() throws Exception
    {
    
        try 
        {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);
            factory.setValidating(true);
            
            SAXParser parser = factory.newSAXParser();
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", "D:\\xintec\\proyecto\\sodimac\\dad\\WLI\\SOD_CORP_EXO_PreruteoEnviar\\SOD_CORP_EXO_PreruteoEnviarReq.xsd");
            
            XMLReader reader = parser.getXMLReader();
            reader.parse(new InputSource("D:\\xintec\\proyecto\\sodimac\\dad\\WLI\\SOD_CORP_EXO_PreruteoEnviar\\instance1.xml"));
            
            return true;
        } 
        catch (ParserConfigurationException e) 
        {
            e.printStackTrace();
            return false;
        } 
        catch (SAXException e) {
            e.printStackTrace();
            return false;
        } 
        catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        
    }
    
    
    
     public static boolean validacion2() throws Exception
    {
    
  SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
        Document document = parser.parse(new File("D:\\xintec\\proyecto\\sodimac\\dad\\WLI\\SOD_CORP_EXO_PreruteoEnviar\\instance1.xml"));

        Schema schema = schemaFactory.newSchema(new File("D:\\xintec\\proyecto\\sodimac\\dad\\WLI\\SOD_CORP_EXO_PreruteoEnviar\\SOD_CORP_EXO_PreruteoEnviarReq.xsd"));
        Validator validator = schema.newValidator();

        validator.validate(new DOMSource(document));
        return true;
    } catch (SAXException e) {
        e.printStackTrace();
        return false;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    }
} 
